<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Author: allen.qiang
// +----------------------------------------------------------------------

/**
 * 标签组ajax类
 * @package  	ajax
 * @subpackage  backend
 * @author   	allen.qiang
 */

quanli_require('module/base/goodsHandler.class.php');

class ajax_tagGroup {

    public function execute() {

        if (isset($_REQUEST['operFlg']) && $_REQUEST['operFlg'] > 0) {

            switch($_REQUEST['operFlg']) {

                case 1: //查询商品标签组
                    $this->searchTagGroup();
                    break;
			    case 2: //添加和修改商品标签组
                    $this->saveTagGroup();
                    break;
				case 4: //删除商品标签组
					$this->deleteTagGroup();
					break;
				case 5: //商品标签组状态
					$this->statusTagGroup();
					break;
                case 6: //待编辑商品标签组查询
                    $this->searchOneTagGroup();
                    break;
                case 7: //查询最大序列号
                    $this->searchSort();
                    break;
            }

        }

    }

    /**
     * 查询商品标签组
     */
    private function searchTagGroup() {

        global $G_PGLOB;
        
        //参数
        $sortname  	  = $_REQUEST['sortname'] ;
        $sortorder 	  = $_REQUEST['sortorder'] ;
        $page	   	  = $_REQUEST['page'] ;
        $rp		   	  = $_REQUEST['rp'] ;
        $tagGroupName = trim($_REQUEST['tagGroupName']) ;
        
        //查询条件
		$arg_array = array(
			'sortname' 	=> $sortname , 
			'sortorder' => $sortorder , 
			'page' 		=> $page , 
			'rp' 		=> $rp 
		);

		//字段查询方式
		$extend_array = array() ;
		if( $tagGroupName != '' ) {
			$arg_array['tagGroupName'] 	   = $tagGroupName ;
			$extend_array ['tagGroupName'] = 1  ; 
		}
		
		$commHandler = new commHandler( $G_PGLOB->DBCA ) ;
		$result_data = $commHandler->search( $arg_array , 'goods_tag_group' , $extend_array ) ;

		$result_array = array(
            'page'  => $arg_array['page'] ,
            'total' => $arg_array['count'] ,
            'rows'  => array()
        );
        
		if ($result_data) {
			$edit_permission = $G_PGLOB->has_permission(514);
            foreach($result_data as &$t_g) {
            	if($edit_permission){
	        		$result_array['rows'][] = array(
	                    'id' 	=> $t_g->tagGroupID,
	                    'cell' 	=> array(
	            			'<a href="javascript: void(0);" onclick="tagGroup.edit(' . $t_g->tagGroupID . ')">修改</a>&nbsp;' ,
	            			$t_g->tagGroupName,
	            			$t_g->memoName,
	            			$t_g->sortOrder,
	            			$t_g->useFlg == 1 ?
	            			'<a href="javascript: void(0);" onclick="tagGroup.doAction(' . $t_g->tagGroupID . ', 0) " >启用</a>' :
	            			'<a href="javascript: void(0);" onclick="tagGroup.doAction(' . $t_g->tagGroupID . ', 1 ) " >禁用</a>'
            			),
	                    
	                );
            	}else{
            		$result_array['rows'][] = array(
	                    'id' 	=> $t_g->tagGroupID,
	                    'cell' 	=> array(
	            			$t_g->tagGroupName,
	            			$t_g->memoName,
	            			$t_g->sortOrder,
	            			$t_g->useFlg == 1 ?
	            			'启用' :
	            			'禁用'
	                    ),
	                    
	                );
            	}
            }
        }

        echo json_encode( $result_array );
    }
    
    /**
     * 标签组保存和更新
     *
     */
    private function saveTagGroup () {
    	global $G_PGLOB;
    	
    	$_REQUEST['creator'] = $G_PGLOB->currentUserID;
    	$goodsHandler = new goodsHandler( $G_PGLOB->DBCA ) ;
    	$result_array = $goodsHandler->saveTagGroup( $_REQUEST ) ;
    	echo json_encode($result_array);
    }
    
	/**
	 * 删除标签组
	 *
	 */
	private function deleteTagGroup () {
		global $G_PGLOB;
		
    	$goodsHandler = new goodsHandler( $G_PGLOB->DBCA ) ;
    	$result_array = $goodsHandler->deleteTagGroup( $_REQUEST ) ;
    	echo json_encode($result_array);
	}
	/**
	 * 修改标签组的使用状态
	 *
	 */
	private function statusTagGroup () {
		global $G_PGLOB;
		
    	$goodsHandler = new goodsHandler( $G_PGLOB->DBCA ) ;
    	$result_array = $goodsHandler->statusTagGroup( $_REQUEST ) ; 
    	echo json_encode($result_array);
	}

    //待编辑信息查询
    private function searchOneTagGroup() {

        global $G_PGLOB;

        $arg_array = array('tagGroupID'=>$_REQUEST['tagGroupID']);
		$commHandler = new commHandler( $G_PGLOB->DBCA ) ;
		$result_data = $commHandler->search( $arg_array , 'goods_tag_group'  ) ;
        $result_array = array(
            'tagGroupID'  =>$result_data[0]->tagGroupID,
            'tagGroupName'=>$result_data[0]->tagGroupName,
            'memoName'    =>$result_data[0]->memoName,
            'sortOrder'   =>$result_data[0]->sortOrder,
            'useFlg'      =>$result_data[0]->useFlg
        );

        echo json_encode( $result_array );
    }

    //最大序列号查询
    private function searchSort(){
        global $G_PGLOB;
        //实例化系统公共类获取最大序列号
        $commHandler = new commHandler( $G_PGLOB->DBCA );
        $sort = $commHandler->getSortNum("goods_tag_group","sortOrder");
        $result = array('sortOrder'=>$sort);

        echo json_encode($result);
    }

}
